
var imgNo = "<img width='15px' src='images/n.png'/>";
var imgYes = "<img width='15px' src='images/y.png'>";

function validateTip(element,css,tipString,status){
    element.css(css);
    element.html(tipString);
    element.attr("validateStatus",status);
}
$(document).ready(function(){


  $(function() {

    var loginName = $("#loginName");
    var password = $("#password");
    var confirmPassword = $("#confirmPassword");
    var email = $("#email");
    var mobile = $("#mobile");
    var identity = $("#identity");
    var name = $("#username");
    var sex = $("#sex");
    var check = $("#check");
    var submit = $("#form");
    var t1 = $("#t1");
    var t2 = $("#t2");
    var t3 = $("#t3");
    var t4 = $("#t4");
    var t5 = $("#t5");
    var t6 = $("#t6");
    var t7 = $("#t7");
    var t8 = $("#t8");
    /**
     * 通过身份证号码得到性别
     *  身份证号码 return 1/0 男/女
     */
    var getSexForCard=function (str) {
        var inputStr = str.toString();
        var sex;
        if (inputStr.length == 18) {
            sex = inputStr.charAt(16);
            if (sex % 2 == 0) {
                return 0;
            } else {
                return 1;
            }
        } else {
            sex = inputStr.charAt(14);
            if (sex % 2 == 0) {
                return 0;
            } else {
                return 1;
            }
        }
    };

        /*
         * 验证
         * 失焦\获焦
         * jquery的方法传递
         */
    loginName.bind("blur", function () {
        //ajax后台验证--userCode是否已存在
        //user.do?method=ucexist&userCode=**
        $.ajax({
            type: "GET",//请求类型
            url: "easybuy/user/userexist",//请求的url
            data: {loginName: loginName.val()},//请求参数
            dataType: "json",//ajax接口（请求url）返回的数据类型
            success: function (data) {//data：返回数据（json对象）
                if (data.loginName == "exist") {//账号已存在，错误提示
                    validateTip(t1, {"color": "red"}, imgNo+" 该用户账号已存在", false);
                } else if(data.loginName =="noexist"){//账号可用，正确提示
                    validateTip(t1, {"color": "green"}, imgYes+" 该账号可以使用", true);
                } else {
                    validateTip(t1, {"color": "red"}, imgNo+" 用户名不能为空" ,false);
                }
            },
            error: function (data) {//当访问时候，404，500 等非200的错误状态码
                validateTip(t1, {"color": "red"}, " 您访问的页面不存在", false);
            }
        });

    }).bind("focus", function () {
        //显示友情提示
        validateTip(t1, {"color": "#666666"}, "* 用户编码是您登录系统的账号", false);
    }).focus();
    /**
     * 密码验证
     */
    password.bind("focus", function () {
        validateTip(t2, {"color": "red"}, "* 请输入密码", false);
    }).bind("blur", function () {
        if (password.val() != null && password.val().length > 5
            && password.val().length < 20) {
            validateTip(t2, {"color": "red"}, imgYes, true);
        } else if(password.val() == null || password.val()==""){
            validateTip(t2, {"color": "red"}, imgNo + " *密码不能为空", false);
        }else{
            validateTip(t2, {"color": "red","border":"red"}, imgNo+" 密码输入不符合规范，请重新输入", false);
        }
    });

    /**
     * 验证确认密码
     */
    confirmPassword.bind("focus", function () {
        validateTip(t3, {"color": "red"}, " *请输入与上面一致的密码", false);
    }).bind("blur", function () {
        if (confirmPassword.val() != null && confirmPassword.val().length > 5
            && confirmPassword.val().length < 20 && confirmPassword.val() == password.val()) {
            validateTip(t3, {"color": "green"},imgYes, true);
        } else if(confirmPassword.val() == null ||password.val()==""){
            validateTip(t3, {"color": "red"}, imgNo + " *请确认密码不能为空", false);
        }else {
            validateTip(t3, {"color": "red"}, imgNo + " *两次密码输入不一致，请重新输入", false);
        }
    });
    /**
     * 邮箱验证
     */

    email.bind("focus", function () {
        validateTip(t4, {"color": "red"}, " * 请输入邮箱地址", false);

    }).bind("blur", function () {
        var re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if (email.val()==null || email.val() == "") {
            validateTip(t4, {"color": "red"}, imgNo+" *邮箱不能为空", false);
        }else if (!re.test(email.val())) {
            validateTip(t4, {"color": "red"}, imgNo+"*邮箱地址格式错误", false);
        } else {
            validateTip(t4, {"color": "red"}, imgYes, true);
        }
    });

    /**
     * 验证手机号
     */
    mobile.bind("focus", function () {
        validateTip(t5, {"color": "red"}, " *请输入手机号码", false);
    }).bind("blur", function () {
        var pattern = /^1[34578]\d{9}$/;
        if (mobile.val() == "" || mobile.val()==null) {
            validateTip(t5, {"color": "red"}, imgNo+" *手机号码不能为空", false);
        } else if (!pattern.test(mobile.val())) {
            validateTip(t5, {"color": "red"}, imgNo+" *手机号码格式错误", false);
        } else {
            validateTip(t5, {"color": "red"}, imgYes, true);
        }
    });

    /*// 验证手机号
    function isPhoneNo(phone) {
        var pattern = /^1[34578]\d{9}$/;
        return pattern.test(phone);
    }*/
    /**
     * 验证身份证
     */
    identity.bind("focus", function () {
        validateTip(t6, {"color": "red"}, " *请输入身份证号", false);
    }).bind("blur", function () {
        var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        sex.val(getSexForCard(identity));
        if (identity.val() == "" || identity.val()==null) {
            validateTip(t6, {"color": "red"}, imgNo+" *身份证不能为空", false);
        } else if (!pattern.test(identity.val())) {
            validateTip(t6, {"color": "red"}, imgNo+" *身份证格式错误", false);
        } else {
            validateTip(t6, {"color": "red"}, imgYes, true);

        }

    });

    name.bind("focus",function(){
        validateTip(t8,{"color": "red"}," *请输入姓名",false);
    }).bind("blur",function(){
        var pattern = /^[\u0391-\uFFE5A-Za-z]+$/;
        if(name.val()=="" || name.val()==null){
            validateTip(t8,{"color": "red"},imgNo+" *姓名不能为空",false);
        }else if(!pattern.test(name.val())){
            validateTip(t8,{"color": "red"}, imgNo +" *输入字符非法",false);
        }else if(name.val().length<2 || name.val().length>20){
            validateTip(t8,{"color": "red"}, imgNo +" *姓名长度错误",false);
        }else {
            validateTip(t8,{"color": "red"}, imgYes,true);
        }
    });

    /**
     * 验证复选框状态
     */
    function checked(){
        if(check.attr("checked")){
            return true;
        }else {
            return false;
        }
    }
    submit.bind("click",function(){
        if(t1.attr("validateStatus") != "true" || loginName.val()==""){
            loginName.blur();
        }else if(t2.attr("validateStatus") != "true"){
            password.blur();
        }else if(t3.attr("validateStatus") != "true"){
            confirmPassword.blur();
        }else if(t4.attr("validateStatus") != "true"){
            email.blur();
        }else if(t5.attr("validateStatus") != "true"){
            mobile.blur();
        }else if(t6.attr("validateStatus") != "true"){
            identity.blur();
        }else if(t8.attr("validateStatus") != "true"){
            name.blur();
        }else if(!checked()){
            validateTip(t7, {"color": "red"}, "请阅读并接受《用户协议》", false);
        }else {
            if(confirm("确认提交")){
                $.ajax({
                    type : "POST",
                    url  : "easybuy/user/insert",
                    data : $('#registForm').serialize(),
                    success: function(result){
                        if(result.regist = "success"){
                            alert("注册成功");
                            window.location.href = "Login.html";
                        }
                    },
                    error: function(){
                        alert("异常");
                    }
                });
            }
        }
    });


 });
});
